package com.rrqs.frame;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.greenrobot.eventbus.EventBus;

import com.rrqs.activity.common.Const;
import com.rrqs.util.Utils;

/**
 * @author 用于Android的http协议网络访问
 */
public class WebHttpHelper {

    /**
     * 根据给定的url地址访问网络，得到响应内容(这里为GET方式访问)
     *
     * @param url 指定的url地址
     */
    public static String getWebContentGet(String url) {
        // 创建一个http请求对象
        HttpGet request = new HttpGet(url);
        // 创建HttpParams以用来设置HTTP参数
        HttpParams params = new BasicHttpParams();
        // 设置连接超时或响应超时
        HttpConnectionParams.setConnectionTimeout(params, 4000);
        HttpConnectionParams.setSoTimeout(params, 4000);
        // 创建一个网络访问处理对象
        HttpClient httpClient = new DefaultHttpClient(params);
        try {
            // 执行请求参数项
            HttpResponse response = httpClient.execute(request);
            // 判断是否请求成功
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // 获得响应信息
                String content = EntityUtils.toString(response.getEntity());
                return content;
            } else {
                // 网连接失败，使用Toast显示提示信息
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 释放网络连接资源
            httpClient.getConnectionManager().shutdown();
        }
        return null;
    }

    /**
     * 根据给定的url地址访问网络，得到响应内容(这里为Post方式访问)
     *
     * @param url 指定的url地址
     */
    public static String getWebContentPost(String url,
                                           List<NameValuePair> params) {
        HttpClient client = new DefaultHttpClient();
        HttpEntity entity = null;
        try {
            HttpPost request = new HttpPost(url);
            SharedPreferences sp = com.rrqs.util.MyApplication.getInstace().getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
            String uid = sp.getString("uid", "");
            String token = sp.getString("token", "");
            if (!TextUtils.isEmpty(token)) {
                params.add(new BasicNameValuePair("token", token));
                params.add(new BasicNameValuePair("emp_id", uid));
            }

            HttpEntity httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
            // HttpEntity httpEntity = new UrlEncodedFormEntity(params);
            request.setEntity(httpEntity);
            HttpResponse response = client.execute(request);
            // 判断请求是否成功
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                if ("-500".equals(result)) {
                    result = "";
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("token", "");
                    editor.putString("uid", "");
                    editor.commit();
                    if (!MyApplication.getInstace().getTopActivity().contains("QsLoginActivity")) {
                        EventBus.getDefault().post(Utils.LOGINELSEWHERE);
                    }
                }
                return result;
            } else {
                // 网连接失败
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 关闭连接，释放资源
            try {
                if (entity != null)
                    entity.consumeContent();
                client.getConnectionManager().shutdown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 登录
     *
     * @param ince
     * @param uname
     * @param pass
     * @return
     */
    public static String login_qs(String ince, String uname, String pass, String code) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("uname", uname));
        params.add(new BasicNameValuePair("pass", pass));
        params.add(new BasicNameValuePair("code", code));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 新任务——待抢单列表
     *
     * @param ince
     * @param lat  ——纬度值
     * @param lng  ——经度值
     * @return
     */
    public static String getOrderList(String ince, String lat, String lng) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("lat", lat));
        params.add(new BasicNameValuePair("lng", lng));
        return getWebContentPost(Const.TOURL, params);
    }

    public static String getOrderList(String ince, String lat, String lng, String uid, String rr_last_id, String help_last_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("lat", lat));
        params.add(new BasicNameValuePair("lng", lng));
        params.add(new BasicNameValuePair("emp_id", uid));
        params.add(new BasicNameValuePair("rr_last_id", rr_last_id));
        params.add(new BasicNameValuePair("help_last_id", help_last_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 待取货列表
     *
     * @param ince
     * @param emp_id ——快递ID
     * @return
     */
    public static String getGetorderList(String ince, String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 配送中列表
     *
     * @param ince
     * @param emp_id ————快递ID
     * @return
     */
    public static String getSendorderList(String ince, String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 订单详情
     *
     * @param ince
     * @param main_id
     * @param emp_id
     * @return
     */
    public static String orderinfo(String ince, String main_id, String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("main_id", main_id));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 点击抢单
     *
     * @param ince
     * @param main_id
     * @param emp_id
     * @return
     */
    public static String getQsQiang(String ince, String main_id, String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("main_id", main_id));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 随意购——点击抢单
     *
     * @param ince
     * @param order_id
     * @param emp_id
     * @param type
     * @return
     */
    public static String getHelpQsQiang(String ince, String order_id, String emp_id, String type) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("order_id", order_id));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("type", type));
        Log.i("yudan", "ince=" + ince + "," + "order_id=" + order_id + "," + "emp_id=" + emp_id + "," + "type=" + type);
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 快递取货,确认取货
     *
     * @param ince
     * @param order_id
     * @param emp_id
     * @return
     */
    public static String getfood(String ince, String order_id, String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("order_id", order_id));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 已完成订单列表
     *
     * @param ince
     * @param emp_id
     * @param page
     * @return
     */
    public static String getoverOrd(String ince, String emp_id, String page,String time) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("page", page));
        params.add(new BasicNameValuePair("time", time));
        params.add(new BasicNameValuePair("status", "1"));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 人人便利确认送达
     *
     * @param ince
     * @param order_id
     * @param emp_id
     * @return
     */
    public static String getQsSendFood(String ince, String order_id,
                                       String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("order_id", order_id));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 随意购确认送达
     *
     * @param ince
     * @param order_id
     * @param emp_id
     * @return
     */
    public static String getQsEXSendFood(String ince, String order_id,
                                         String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("order_id", order_id));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.SYGTOURL, params);
    }

    /**
     * 获取骑士信息
     *
     * @param ince
     * @param order_id
     * @param emp_id
     * @return
     */
    public static String getQsinfo(String ince, String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 设置快递员开工收工
     *
     * @param ince
     * @param emp_id
     * @param close
     * @return
     */
    public static String setqsOpen(String ince, String emp_id, String close) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("close", close));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 更新骑士坐标
     *
     * @param ince
     * @param emp_id
     * @param lat
     * @param lng
     * @return
     */
    public static String setEmpLocation(String ince, String emp_id, String lat,
                                        String lng) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("lat", lat));
        params.add(new BasicNameValuePair("lng", lng));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 获取订单数量
     *
     * @param ince
     * @param emp_id
     * @return
     */
    public static String getOrderCount(String ince, String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 配送员提现申请
     *
     * @param ince
     * @param emp_id
     * @param amount
     * @return
     */
    public static String getDeposit(String ince, String emp_id, String amount) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("amount", amount));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 获取配送员账户明细------配送员提现申请
     *
     * @param ince
     * @param emp_id
     * @param page
     * @return
     */
    public static String getEmpAccountlog(String ince, String emp_id,
                                          String page) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("page", page));
        return getWebContentPost(Const.TOURL, params);
    }


    /**
     * 随意购订单详情
     *
     * @param ince
     * @param order_id
     * @param emp_id
     * @return
     */
    public static String sygorderinfo(String ince, String order_id,
                                      String emp_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("order_id", order_id));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 万能跑腿取货,确认取货
     *
     * @param ince
     * @param order_id
     * @param emp_id
     * @return
     */
    public static String exgetfood(String ince, String order_id, String emp_id, String type) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("order_id", order_id));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("type", type));
        return getWebContentPost(Const.SYGTOURL, params);
    }

    /**
     * 已完成万能跑腿订单列表
     *
     * @param ince
     * @param emp_id
     * @param page
     * @return
     */
    public static String getsygoverOrd(String ince, String emp_id, String page) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("page", page));
        return getWebContentPost(Const.SYGTOURL, params);
    }


    /**
     * 获取新版本的信息
     *
     * @param ince
     * @return
     */
    public static String getNewVersion(String ince) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 获取骑士排行榜统计数据
     *
     * @param ince
     * @return
     */
    public static String getqstotalph(String ince, String time, String type) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("status", time));
        params.add(new BasicNameValuePair("type", type));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 获取单个骑士统计数据
     *
     * @param ince
     * @return
     */
    public static String getoneqstotal(String ince, String empid, String time,String status) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", ince));
        params.add(new BasicNameValuePair("emp_id", empid));
        if(!time.equals("")) {
            params.add(new BasicNameValuePair("time", time));
        }
        if(!status.equals("")) {
            params.add(new BasicNameValuePair("status", status));
        }
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 获取异常原因标签
     *
     * @return
     */
    public static String getAbnormalReason() {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", "get_abnormal_reason"));
        return getWebContentPost(Const.TOURL, params);
    }

    /**
     * 确定上报
     *
     * @return
     */
    public static String post_abnormal(String emp_id,String mark_id,String reason,String order_id) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ince", "post_abnormal"));
        params.add(new BasicNameValuePair("emp_id", emp_id));
        params.add(new BasicNameValuePair("mark_id", mark_id));
        params.add(new BasicNameValuePair("reason", reason));
        params.add(new BasicNameValuePair("order_id", order_id));
        return getWebContentPost(Const.TOURL, params);
    }

}